import os
import sys
sys.path.append("..")
from settings import Config

import signal
import subprocess
import shlex

def termination_signal_handler(signum, frame):
    print ('catched terminate singal: %d' % signum)
    globals_child.kill()
    sys.exit()

if __name__ == "__main__":
    config = Config()
    signal.signal(signal.SIGTERM, termination_signal_handler)   #kill pid
    signal.signal(signal.SIGINT, termination_signal_handler)     #ctrl -c
    #signal.signal(signal.SIGKILL, term_sig_handler)  #kill -9 pid  此功能无效

    server_file_path = "snowflake_start_server"
    if os.name == "posix":
        server_file_path = "/home/slave3/LoadData/venv/bin/snowflake_start_server"

    command = "{server_file_path} --address={host} --dc=0 --debug={debug}  --port={port} --worker=0".format(
        server_file_path = server_file_path,
        host = config.SNOWFLAKE_HOST,
        debug = config.SNOWFLAKE_DEBUG,
        port = config.SNOWFLAKE_PORT,
    )

    print(command)

    args = shlex.split(command)

    global globals_child

    globals_child = subprocess.Popen(args)

    globals_child.wait()
